//
//  HMHomeRecommendDetailVC.m
//  HowToDo
//
//  Created by Stan on 2017-01-03.
//  Copyright © 2017 KY. All rights reserved.
//

#import "HMHomeRecommendDetailTVC.h"
#import <Masonry.h>
#import "UIColor+Addition.h"
#import <YYModel.h>
#import "UIImage+ImageEffects.h"
#import "HMRecommendDetailVC.h"
#import "HMHomeRecommendListModel.h"
#import "HMRecommendListCell.h"

@interface HMHomeRecommendDetailTVC ()
@property(strong,nonatomic)UILabel *resourceNameLabel;
@property(strong,nonatomic)UILabel *resourceEnclosureLabel;
@property(strong,nonatomic)UIImageView *coverImageView;
@property(strong,nonatomic)UILabel *resourceContentLabel;
@property(strong,nonatomic)UIImageView *coverBackgroudImageView;
//放相关推荐列表中cell的数组模型
@property(strong,nonatomic)NSArray<HMHomeRecommendListModel *> *listModelArray;



@end

static NSString *TableViewCellIdentifier= @"TableViewCellIdentifier";
static CGFloat defaultMargin = 21.0;
@implementation HMHomeRecommendDetailTVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    创建头部视图
    self.tableView.tableHeaderView = [self createHeaderView];
    
    
    
    [self.tableView registerClass:[HMRecommendListCell class] forCellReuseIdentifier:TableViewCellIdentifier];
    
    self.tableView.estimatedRowHeight = 120;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}


- (UIView *)createHeaderView{
    UIView *headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor whiteColor];
    
    UIView *coverView = [[UIView alloc]init];
    [headerView addSubview:coverView];
    
    [coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.right.offset(0);
//        make.height.mas_lessThanOrEqualTo(284);
        make.height.offset(284);
        
        
    }];
    
//    设置封面背景图层
    UIImageView *coverBackgroudImageView = [[UIImageView alloc]init];
    [coverView addSubview:coverBackgroudImageView];
    self.coverBackgroudImageView = coverBackgroudImageView;
    
//    高斯模糊背景颜色
    UIImage *blurImage = [[UIImage imageNamed:@"201612271441573272"] applyExtraLightEffect];
    coverBackgroudImageView.image = blurImage;
    [coverBackgroudImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
//    设置封面
    UIImageView *coverImageView = [[UIImageView alloc]init];
    
    [coverBackgroudImageView addSubview:coverImageView];
    self.coverImageView = coverImageView;
    
    coverImageView.image = [UIImage imageNamed:@"201612271441573272"];
    
    //    给照片添加阴影
    coverImageView.layer.shadowColor = [UIColor darkGrayColor].CGColor;
    coverImageView.layer.shadowOffset = CGSizeMake(-5, -5);
    coverImageView.layer.shadowOpacity = 0.4;
    coverImageView.layer.shadowRadius = 10.0;
    
    
    [coverImageView sizeToFit];
    coverImageView.contentMode = UIViewContentModeScaleAspectFit;
    [coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(30);
        make.left.equalTo(coverView.mas_left).offset(defaultMargin);
        make.right.equalTo(coverView.mas_right).offset(-defaultMargin);
        make.height.mas_lessThanOrEqualTo(225);
//        make.height.offset(225);
    }];
    
    
//    设置标题
    self.resourceNameLabel = [UILabel hm_labelWithText:@"11.11专治各种想不开| 川普凭什么赢？秘密全在这里" fontSize:k_FONT_NORMAL colorWithHex:0x323232];
//    加粗
    self.resourceNameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:k_FONT_NORMAL];
    [headerView addSubview:self.resourceNameLabel];

    self.resourceNameLabel.numberOfLines = 0;
    
    [self.resourceNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(coverView.mas_bottom).offset(35);
        make.left.offset(defaultMargin);
        make.right.offset(- defaultMargin);
    }];
    
    //    标题下面的横线
    UIView *lineView = [[UIView alloc]init];
    [headerView addSubview:lineView];
    lineView.backgroundColor = [UIColor colorWithHex:0xdddddd];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.resourceNameLabel.mas_bottom).offset(defaultMargin);
        make.left.offset(defaultMargin);
        make.right.offset(-defaultMargin);
        make.height.offset(1);
    }];
    
    UILabel *audioDesLabel = [[UILabel alloc]init];
    [headerView addSubview:audioDesLabel];
    audioDesLabel.text = @"音频简介";
    audioDesLabel.font = [UIFont systemFontOfSize:14.0];
    audioDesLabel.textColor = [UIColor colorWithHex:0x323232];
    
    [audioDesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_bottom).offset(defaultMargin);
        make.left.offset(defaultMargin);
    }];
    
    //    音频时长
    self.resourceEnclosureLabel = [UILabel hm_labelWithText:@"音频时长：13分22秒" fontSize:k_FONT_NORMAL colorWithHex:0x323232];
    [headerView addSubview:self.resourceEnclosureLabel];
    
    
    [self.resourceEnclosureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(audioDesLabel.mas_centerY).offset(0);
        make.right.offset(-defaultMargin);
    }];
    
    
//    创建简介
    self.resourceContentLabel = [UILabel hm_labelWithText:@"11.11专治各种想不开| 川普凭什么赢？秘密全在这里" fontSize:k_FONT_SMALL colorWithHex:0x323232];
    self.resourceContentLabel.numberOfLines = 1;
    [headerView addSubview:self.resourceContentLabel];
    
    
    [self.resourceContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.resourceEnclosureLabel.mas_bottom).offset(defaultMargin);
        make.left.offset(defaultMargin);
        make.right.offset(-defaultMargin);
    }];
    
    //    简介下面的横线
    UIView *contentLineView = [[UIView alloc]init];
    [headerView addSubview:contentLineView];
    contentLineView.backgroundColor = [UIColor colorWithHex:0xdddddd];
    
    [contentLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.resourceContentLabel.mas_bottom).offset(defaultMargin);
        make.left.right.offset(0);
        make.height.offset(1);
    }];
    
    [headerView layoutIfNeeded];
    
    headerView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, CGRectGetMaxY(contentLineView.frame));
    
    return headerView;
}


////设置组头高度
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 50;
//}
//
////设置组头的view
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView *sectionHeaderView = [[UIView alloc]init];
//    sectionHeaderView.backgroundColor = [UIColor whiteColor];
//    
//    UILabel *sectionLabel = [[UILabel alloc]init];
//    [sectionHeaderView addSubview:sectionLabel];
//    sectionLabel.text = @"相关推荐";
//    
//    [sectionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.offset(defaultMargin);
//        make.left.offset(defaultMargin);
//        
//    }];
//    
//    return sectionHeaderView;
//}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"相关推荐";
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listModelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HMRecommendListCell *cell = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier forIndexPath:indexPath];
    
    cell.listModel = self.listModelArray[indexPath.row];

    
    return cell;
}

//选中相关推荐中cell，进行跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HMRecommendDetailVC *detailTVC = [[HMRecommendDetailVC alloc]init];
    
    detailTVC.resource_id = self.listModelArray[indexPath.row].resource_id;
    
    [self.navigationController pushViewController:detailTVC animated:YES];
}


- (void)setResource_id:(NSString *)resource_id{
    _resource_id = resource_id;
    
    [HMRecommendCellModel loadDetailDataWithDetailID:resource_id.integerValue andFinished:^(id responseObject) {
        self.recommendCellModel = responseObject;
         [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:self.recommendCellModel.image_url] placeholderImage:[UIImage imageNamed:@"201612271441573272"]];
#warning  更改了 显示封面图片的URL by 曲超

        self.resourceNameLabel.text = self.recommendCellModel.resource_name;
        self.resourceEnclosureLabel.text = [NSString getTimeWithString:self.recommendCellModel.resource_enclosure];
        self.resourceContentLabel.text = self.recommendCellModel.resource_content;
        
        self.coverBackgroudImageView.image = [self.coverImageView.image applyExtraLightEffect];
//        对相关推荐中的数组进行模型转换
        self.listModelArray = [NSArray yy_modelArrayWithClass:[HMHomeRecommendListModel class] json:self.recommendCellModel.abouts];
        
        
//        获取到数据之后，需要刷新tableview。不然会出现NULL。
        [self.tableView reloadData];
    } Failed:^(NSError *error) {
        
        HMLog(@"error = %@",error);
        
    }];
}

#pragma 懒加载的方法
- (NSArray<HMHomeRecommendListModel *> *)listModelArray{
    if (_listModelArray == nil) {
        _listModelArray = [[NSArray alloc]init];
    }
    return _listModelArray;
}

@end
